import 'package:flutter/material.dart';
import 'package:ichinesereader/exam/dto/assessment.response.dart';
import 'package:ichinesereader/exam/util/ExamUtil.dart';
import 'package:ichinesereader/exam/widgets/SampleAudioPlayer.dart';

import 'BGExamWidget.dart';

/// 用法与BGExamWidget基本一致，完全包含BGExamWidget.
/// Sample:
/// BGExamSectionWidget(questionSection: section)
/// 可在Section外加各种装饰
//   Container(
//         margin: EdgeInsets.only(top: 20),
//         padding: EdgeInsets.fromLTRB(10, 20, 10, 20),
//         decoration: BoxDecoration(
//             borderRadius: BorderRadius.circular(6),
//             border: Border.all(
//               color: Get.isDarkMode ? AppTheme.colors.darkTheme2 : Color(0xffDEE2E6),
//               width: 1,
//             ),
//             color: Get.isDarkMode ? Colors.white10 : Colors.white,
//             boxShadow: [
//               BoxShadow(blurRadius: 2, spreadRadius: 2, offset: Offset(0, 2), color: Colors.black.withOpacity(0.1)),
//             ]),
//         child: BGExamSectionWidget(
//             questionSection: section, mode: BGExamWidgetMode.Practice, isRecordSmall: true, keyList: controller.keyList[sectionNum]))
/// 如需加题号可 赋值item.head?.questionTitleNum，此字段默认为‘’，可参考如下方法添加题号
// for (int i = 0; i < assessmentResult!.list!.length; i++) {
//         QuestionPart? part = assessmentResult!.list?[i];
//         for (int j = 0; j < part!.questions!.length; j++) {
//           QuestionSection section = part.questions![j];
//           int sectionNum = 0;
//           if (i > 0) {
//             sectionNum = i;
//           } else {
//             if (j > 0) sectionNum = j;
//           }
//           String sectionNumStr = (sectionNum + 1).toString() + '.';
//           for (int k = 0; k < section.question!.length; k++) {
//             QuestionItem item = section.question![k];
//             item.head!.questionTitleNum = sectionNumStr + ' ';
//             if (section.question!.length > 1) {
//               item.head!.questionTitleNum = sectionNumStr + (k + 1).toString() + ' ';
//             }
//           }
//         }
//       }



class BGExamSectionWidget extends StatefulWidget {
  BGExamSectionWidget({
    Key? key,
    required this.questionSection, //QuestionSection section级别的数据
    this.getAnswer, // 获取answer回调，一般情况用不到回调，可直接从item.answer中获取
    this.isRecordSmall, // 使录音控件排列更紧凑，只占用一行
    this.keyList, // GlobalKey的list
    this.isShowJudge,
    this.switchSave,
    this.mode = BGExamWidgetMode.Practice, // 默认值是Practice
  }) : super(key: key);
  final QuestionSection questionSection;
  final BGExamWidgetMode mode;
  final bool? isRecordSmall;
  final Function(String)? getAnswer;
  final List<GlobalKey>? keyList;
  // 是否显示评判结果  (一般用于字卡题，直接显示答案是否正确)
  // choice字形选择题，默认值：true
  // option选择题，   默认值：false
  final bool? isShowJudge;
  final bool? switchSave;

  @override
  State<BGExamSectionWidget> createState() => _BGExamWidgetState();
}

class _BGExamWidgetState extends State<BGExamSectionWidget> {
  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    QuestionSection section = widget.questionSection;
    List<Widget> options = [];
    if(section.content!.show != null){
      if(section.content!.audio != null){
        section.content!.json = section.content!.audio;
        section.content!.audio = null;
      }
      String audioUrl = section.content!.json ?? '';
      options.add(Padding(padding: EdgeInsets.only(bottom: 15), child: ExamUtil.buildHeadInfoWidget(section.content, context)));
      if(audioUrl != ''){
        options.add(SampleAudioPlayer(name: 'audioPlayerExam', audioUrl: audioUrl));
        options.add(SizedBox(height: 15));
      }
    }

    for (int i = 0; i < section.question!.length; i++) {
      QuestionItem item = section.question![i];
      options.add(Padding(
          padding: EdgeInsets.only(bottom: 20),
          child: BGExamWidget(
              key: (widget.keyList == null) ? null : widget.keyList![i],
              questionItem: item,
              mode: widget.mode,
              isRecordSmall: widget.isRecordSmall,
              questionId: section.id,
              isShowJudge: widget.isShowJudge,
              switchSave:widget.switchSave,
              getAnswer: widget.getAnswer)));
    }
    return Column(children: options);
  }
}
